<?php

class ApiController extends ControllerBase
{
    public static function upload($user_id,$from,$height,$width)
    {
        
        return \Cloudinary\Uploader::upload($from,
        array(
           "public_id" => $user_id,
           "height" => $height,
           "width" => $width,
           "invalidate" => true
        ))['version'];

    }


    public function indexAction()
    {

        

    }   
    public function getInfoAction()
    {
        $user_id = $_GET['user_id'];
        $result = array();
        
        $info = UserAccountTb::findFirst(array(
            "conditions"=>"id = ?0",
            "bind"=>array(0=>$user_id),
            "columns"=>"id,fname,lname,profile_pic"
            ));
        if($info)
        {
            $result['status'] = true;
            $result['data'] = $info;
        }
        echo json_encode($result);
        $this->view->disable();

    }

    public function uploadAction($method='')
    {
        $path = $this->request->getPost('path');
        $id = $this->request->getPost('id');
        $user_id = $this->session->get('user_id');
        $result = array();

        switch($method)
        {
            case "profile_pic":
                $user = UserAccountTb::findFirstById($user_id);

                $up = $this->upload($id,$path,PROF_HEIGHT,PROF_WIDTH);
                if(!$up)
                {
                    $result['status'] = false;
                    $result['messages'] = "There is a problem in uploading your photo pls try again later";
                }
                else
                {
                    $user->profile_pic = $up.'/profile_pic_'.$user_id.'.jpg';
                    if($user->save() == false)
                    {
                        $result['status'] = false;
                        $result['messages'] = "There is a problem in saving your photo pls try again later";       
                    }
                    else
                    {
                        $this->session->set('profile_pic', $up.'/profile_pic_'.$user_id.'.jpg');    
                        $result['status'] = true;     
                    }
                }

            break;
        }
        

        echo json_encode($result);
        $this->view->disable();

    }
    public function emailAction($method="")
    {
    	$result = array();

    	switch($method)
    	{
    		case "resend":
    		if($this->request->isPost() == true)
    		{
    			$result['status'] = UsersController::sendRegistrationEmail($_POST['email'],$_POST['activation_key']);
    		}
    		
    		break;

    	}
    	

    	echo json_encode($result);
    	$this->view->disable();

    }



}

